﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MisprintTestingTool
{
    public partial class RegistrationForm : Form
    {
        bool allParamsIsEnter;   // заполнены ли все необходимые поля (студент/не студент)    

        public RegistrationForm()
        {
            InitializeComponent();
            StudentPanel.Visible = false;
            WokerPanel.Visible = true;
        }

        private void SetUserData()
        {
            Program.userInfo.surname = textBoxSurname.Text;
            Program.userInfo.name = textBoxName.Text;
            Program.userInfo.middleName = textBoxMiddleName.Text;
            Program.userInfo.experienceSkillsPC = numericUpDownExperienceSkillsPC.Value.ToString();

            if (checkBox_student.Checked) // если студент
            {
                Program.userInfo.isStudent = true;
                Program.userInfo.role = "student";
                Program.userInfo.faculty = comboBoxFaculty.Text;
                Program.userInfo.specialty = comboBoxSpecialty.Text;
                Program.userInfo.course = numericUpDownCourse.Value.ToString();
            }
            else // если не студент
            {
                Program.userInfo.role = "worker";
                Program.userInfo.isStudent = false;
                Program.userInfo.job = textBoxJob.Text;
                Program.userInfo.seniorityWork = numericUpDownSeniorityWork.Value.ToString();
            }
        }

        private void FillComboBox(ref ComboBox lst, List<string> arr)
        {
            foreach (var text in arr)
            {
                lst.Items.Add(text);
            }
        }

        private void ShowInfoAboutFields()
        {
            MessageBox.Show("Пожалуйста, заполните все поля для прохождения теста.", "Заполнены не все поля", MessageBoxButtons.OK, MessageBoxIcon.Information);
            allParamsIsEnter = false;
        }

        private void CkeckFields()
        {
            allParamsIsEnter = true;
            bool commonFieldsIsOK = true;

            if ((Program.userInfo.surname.Length == 0) || (Program.userInfo.name.Length == 0) || (Program.userInfo.middleName.Length == 0))
            {
                ShowInfoAboutFields();
                commonFieldsIsOK = false;
            }
            else if (checkBox_student.Checked && commonFieldsIsOK)
            {
                if ((Program.userInfo.faculty.Length == 0) || (Program.userInfo.specialty.Length == 0) || (Program.userInfo.course.Length == 0))
                {
                    ShowInfoAboutFields();
                }
            }
            else if (!checkBox_student.Checked && commonFieldsIsOK)
            {
                if ((Program.userInfo.job.Length == 0) || (Program.userInfo.seniorityWork.Length == 0))
                {
                    ShowInfoAboutFields();
                }
            }
        }

        private void RegistrationForm_Shown(object sender, EventArgs e)
        {
            FillComboBox(ref comboBoxFaculty, DBFacultySpecialty.GetFacultyList());
        }

        private void comboBoxFaculty_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxSpecialty.Items.Clear();
            string code = comboBoxFaculty.SelectedItem.ToString();
            FillComboBox(ref comboBoxSpecialty, DBFacultySpecialty.GetSpecialtyList(code));
        }

        private void checkBox_student_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_student.Checked)
            {
                StudentPanel.Visible = true;
                WokerPanel.Visible = false;
            }
            else
            {
                StudentPanel.Visible = false;
                WokerPanel.Visible = true;
            }
        }

        private void btnPerformTest_Click(object sender, EventArgs e)
        {
            // заполнение информации о пользователе
            SetUserData();

            // проверка заполненности всех полей
            CkeckFields();

            if (allParamsIsEnter)
            {
                this.Visible = false;
                (new MainForm()).ShowDialog();
                Close();
            }
        }
    }
}
